package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.QuestionVOsBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.PreviewHomeworkAdapter;
import com.fancy.learncenter.ui.view.recycleviewgallery.ScalableCardHelper;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 预览作业
 */
public class TeacherPreviewHomeworkActivity extends BaseActivity implements ScalableCardHelper.OnPageChangeListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.right_img)
    ImageView rightImg;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.bottom_layout)
    RelativeLayout bottomLayout;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    List<QuestionVOsBean> list = new ArrayList<>();
    PreviewHomeworkAdapter adapter;
    ScalableCardHelper cardHelper;
    int currentPosition;
    Handler handler = new Handler();

    @Bind(R.id.index_question)
    TextView indexQuestion;
    @Bind(R.id.preview_question_tv)
    TextView previewQuestionTv;
    @Bind(R.id.next_question_tv)
    TextView nextQuestionTv;


    private boolean isToOriginData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_preview_homework, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("作业预览");
        getIntentData();
        initRecycleView();
        initData();
        initClick();
        indexQuestion.setText("1/" + list.size());
        nextQuestionTv.setBackgroundResource(R.drawable.cartoon_teacher_next_preview_light_btn);
        previewQuestionTv.setBackgroundResource(R.drawable.cartoon_teacher_next_preview_light_btn);


        previewQuestionTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtil.MyLog("index_question", "====index_question===");
                if (currentPosition >= 1) {
                    recyclerView.smoothScrollToPosition(currentPosition - 1);
                }
            }
        });
        nextQuestionTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentPosition < list.size() - 1) {
                    recyclerView.smoothScrollToPosition(currentPosition + 1);
                }
            }
        });
    }

    private void getIntentData() {
        Bundle bundle = getIntent().getExtras();
        list = bundle.getParcelableArrayList(JumpIntentKey.QUSETION_BANK_LIST_KEY);
    }


    private void initClick() {
    }

    private void initRecycleView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        adapter = new PreviewHomeworkAdapter(this, list);
        recyclerView.setAdapter(adapter);

        cardHelper = new ScalableCardHelper(this);
        cardHelper.attachToRecyclerView(recyclerView);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);  //取消局部刷新，出现闪屏的问题

    }

    private void initData() {
        notifyButtonStatus(0);
    }

    private void notifyButtonStatus(int position) {

//        if (position == 0) {
//

        previewQuestionTv.setEnabled(true);
//
//            previewQuestion.setBackgroundResource(R.drawable.cartoon_teacher_next_preview_gray_btn);
////            previewQuestion.setEnabled(false);
//
//            previewQuestion.setVisibility(View.GONE);
//        } else if (position == list.size() - 1) {
//            nextQuestion.setBackgroundResource(R.drawable.cartoon_teacher_next_preview_gray_btn);
////            nextQuestion.setEnabled(false);
//

        nextQuestionTv.setEnabled(true);
//        }


        currentPosition = position;


        indexQuestion.setText((position + 1) + "/" + list.size());
    }


    @Override
    public void onPageSelected(final int position) {
        Log.i("stop------", "stop+++++");

        if (adapter != null) {
            Utils.stopVoiceImage(adapter.getVoice_image());
            if (adapter.getAudioPlayer() != null && adapter.getAudioPlayer().isPlaying()) {

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("stop------", "stop------");
                        adapter.getAudioPlayer().stop();
                        adapter.setAudioPlayer(null);
                    }

                }, 500);
            }
            notifyButtonStatus(position);
            if (list != null) {
                for (QuestionVOsBean bean : list) {
                    bean.setIsChecked(0);
                }
                if (position > 1) {
                    adapter.notifyItemChanged(position);
                    adapter.notifyItemChanged(position - 1);
                    title.setText(list.get(position).getConstructionNameCn());
                } else if (position == 0) {
                    adapter.notifyItemChanged(position);
                }
            }
        }


    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.index_question:

                break;
        }
    }
}
